package com.ggs.starbug.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author lianghaohui
 * @Date 2021/10/13 18:12
 * @Description
 */
public class TestData {
    /**
     * in -121
     *
     * @param x
     * @return
     */
    public static boolean isPalindrome(int x) {
        if (x == 0) return true;
        if (x < 0 || x % 10 == 0) return false;
        int reversed = 0;
        /**
         * 12345
         * 5    1234
         * 54   123
         * 543  12
         *
         * 12321
         * 1    1232
         * 12   123
         * 123  12
         *
         * 123321
         * 1    12332
         * 12   1233
         * 123  123
         */
        while (x > reversed) {
            reversed = reversed * 10 + x % 10;
            x /= 10;
        }
        int[] k = new int[]{1, 2};
        return x == reversed || x == reversed / 10;
    }


    public static int[] twoSum(int[] nums, int target) {
        int index1, index2;
        for (int i = 0; i < nums.length; i++) {
            for (int k = 0; k < nums.length; k++) {
                int result = nums[i] + nums[k];
                if (result == target && i != k) {
                    index1 = i;
                    index2 = k;
                    return new int[]{index1, index2};
                }
            }
        }
        return new int[]{};
    }

    public static int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] arr = new int[2];
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                arr[0] = map.get(nums[i]);
                arr[1] = i;
                return arr;
            }
            map.put(target - nums[i], i);
        }
        return arr;
    }


    public static void main(String[] args) {
        ListNode node1_7 = new ListNode(9, null);
        ListNode node1_6 = new ListNode(9, node1_7);
        ListNode node1_5 = new ListNode(9, node1_6);
        ListNode node1_4 = new ListNode(9, node1_5);
        ListNode node1_3 = new ListNode(9, node1_4);
        ListNode node1_2 = new ListNode(9, node1_3);
        ListNode node1_1 = new ListNode(9, node1_2);
        ListNode head1 = node1_1;
        while (head1 != null) {
            System.out.print(head1.val);
            head1 = head1.next;
        }

        System.out.println();
        ListNode node2_4 = new ListNode(9, null);
        ListNode node2_3 = new ListNode(9, node2_4);
        ListNode node2_2 = new ListNode(9, node2_3);
        ListNode node2_1 = new ListNode(9, node2_2);
        ListNode head2 = node2_1;
        while (head2 != null) {
            System.out.print(head2.val);
            head2 = head2.next;
        }

        System.out.println();
        ListNode listNode = addTwoNumbers(node1_1, node2_1);
        while (listNode != null) {
            System.out.print(listNode.val);
            listNode = listNode.next;
        }
        System.out.println();
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int node1Num = collectNum(l1);
        int node2Num = collectNum(l2);
        if (node1Num == 0 && node2Num == 0) {
            return new ListNode(0);
        }
        int count = node1Num + node2Num;
        ListNode totalNode = new ListNode();
        while (count != 0) {
            int cur = count % 10;
            if (totalNode.next == null) {
                totalNode.next = new ListNode(cur, null);
            } else {
                ListNode curNode = new ListNode(cur, totalNode.next);
                totalNode.next = curNode;
            }
            count = count / 10;
        }
        return reverseList(totalNode.next);
    }

    public static ListNode reverseList(ListNode node) {
        ListNode reverseList = new ListNode();
        while (node != null) {
            ListNode cur = node;
            node = node.next;
            if (reverseList.next == null) {
                reverseList.next = cur;
                cur.next = null;
            } else {
                cur.next = reverseList.next;
                reverseList.next = cur;
            }
        }
        return reverseList.next;
    }

    public static int collectNum(ListNode node) {
        int sum = 0;
        while (node != null) {
            sum = sum * 10 + node.val;
            node = node.next;
        }
        return sum;
    }

}
